﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

#endregion


namespace ScrumTable.UI.View.Converter
{


    /// <summary>
    /// Convert string -> ImageSource
    /// Checks if a imageSource is as content available the content gehts return otherwise the ressource file. 
    /// </summary>
    [ValueConversion(typeof(string), typeof(ImageSource))]
    public class ConverterContentOrRessourceImage : IValueConverter
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Converts a string to a imagesource.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            const string split = ";component/";
            ImageSourceConverter imgs=new ImageSourceConverter();
            ImageSource source = null;
            string url = parameter.ToString();
            url = url.Substring(url.IndexOf(split) + split.Length);
            Uri uri = new Uri(url, UriKind.Relative);
            string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if( File.Exists( Path.Combine(baseDir, url)))
            {
                source = (ImageSource)imgs.ConvertFromString(uri.ToString());
            }
            else
            {
                source = ((ImageSource)imgs.ConvertFromString(parameter.ToString()));
            }
            source.Freeze();
            return source;

        }


        /// <summary>
        /// Not supported
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

      
        #endregion
       
    }
}
